package com.seven.restaurant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.seven.restaurant.entity.po.UserPO;
import com.seven.restaurant.entity.request.LoginRequest;
import com.seven.restaurant.entity.vo.UserDetailsVO;
import com.seven.restaurant.exception.GlobalException;
import com.seven.restaurant.exception.InfoCode;
import com.seven.restaurant.service.UserService;
import com.seven.restaurant.util.FTPUtil;
import com.seven.restaurant.util.ResponseEntity;
import io.swagger.annotations.*;
import org.apache.commons.lang3.tuple.MutablePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author BaiBan
 * @since 2022-07-16
 */
@RestController
@RequestMapping("/user")
@Api(value="用户",tags = {"用户相关的接口"})
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    FTPUtil ftp;

    /**
     * 登录
     * @param loginRequest {@link LoginRequest}
     * @return 用户id (凭证)
     */
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody LoginRequest loginRequest) throws GlobalException {
        Long userId = userService.login(loginRequest);
        return ResponseEntity.success(new MutablePair<>("userId", userId));
    }

    /**
     * 注册
     * @param user {@link UserPO}
     */
    @ApiOperation(value ="注册", notes = "所有类型用户注册")
    @PostMapping("/register")
    public ResponseEntity<?> register(@RequestBody UserPO user) throws GlobalException {
        Long userId = userService.register(user);
        return ResponseEntity.success(new MutablePair<>("userId", userId));
    }

    /**
     * 员工详情
     * @param id 编号
     * @return {@link UserDetailsVO}
     */
    @ApiOperation(value ="详情", notes = "用户详情")
    @ApiImplicitParam(name="id", value="用户编号", required=true, paramType="query")
    @GetMapping("/details")
    public ResponseEntity<UserDetailsVO> details(Integer id) throws GlobalException {
        UserDetailsVO vo = userService.details(id);
        return ResponseEntity.success(vo);
    }

    /**
     * 上传头像
     * @param file 头像图片
     * @return 头像url
     * @throws GlobalException 全局异常
     */
    @ApiOperation(value ="头像上传", notes = "上传用户头像")
    @ApiImplicitParam(name="file", value="菜品图片", required=true, paramType = "form", dataType="__file")
    @PostMapping("/header")
    public ResponseEntity<?> upload(@RequestParam MultipartFile file) throws GlobalException {

        if (file == null || file.isEmpty())
            throw new GlobalException(InfoCode.FILE_NULL_ERROR);

        String urlStr = ftp.uploadFile(file, "header");
        return ResponseEntity.success(new MutablePair<>("icon", urlStr));
    }

    /**
     * 返回 用户列表
     * @param current 页数
     * @return 分页后的列表
     */
    @ApiOperation(value ="用户列表", notes = "分页查询")
    @ApiImplicitParam(name="current", value="页数", paramType="query")
    @ApiResponse(code=200, message="请求处理成功", response = UserDetailsVO.class)
    @GetMapping("/list")
    public ResponseEntity<IPage<UserDetailsVO>> list(@RequestParam(value = "current", defaultValue = "1")Integer current) {
        return ResponseEntity.success(userService.list(current));
    }

    /**
     * 按照id删除用户
     * @param id 用户id
     * @return 删除是否成功
     */
    @ApiOperation("删除用户")
    @ApiResponses({
            @ApiResponse(code=225, message="数据删除成功"),
            @ApiResponse(code=425, message="数据删除失败"),
    })
    @PostMapping("delete")
    public ResponseEntity<?> delete(@RequestBody Integer id) {
        return ResponseEntity.delete(userService.removeById(id));
    }

    /**
     * 修改 用户信息
     * @param user 用户修改后的信息
     * @return 修改是否成功
     */
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    @ApiResponses({
            @ApiResponse(code=230, message="数据更新成功"),
            @ApiResponse(code=430, message="数据更新失败"),
    })
    @PostMapping("update")
    public ResponseEntity<?> update(@RequestBody UserPO user) {
        return ResponseEntity.update(userService.updateById(user));
    }
}